<?php


function get_userInfo($user_id=0)
{
	$user_info = array();
	$sql="SELECT * FROM " .$GLOBALS['db']->table('users')." WHERE user_id='$user_id' LIMIT 1";
	$res = $GLOBALS['db']->getRow($sql);
	if($res){
      $user_sex = array('未知','男','女');
      $user_info['user_id']    = $res['user_id'];
      $usericon = GZ_Api::get_url('/Mapi/public/images/usericon.png');
      $user_info['user_headimg']   = empty($res['headimg'])?$usericon:GZ_Api::get_url($res['headimg']);
      $user_info['user_name']    = empty($res['user_name'])?'未设置':$res['user_name'];
      $user_info['user_sex']   = $user_sex[$res['sex']];
      $user_info['user_age']   = empty($res['age'])?'':$res['age'];
      $user_info['height']   = empty($res['height'])?'':$res['height'];//身高
      $user_info['weight']   = empty($res['weight'])?'':$res['weight'];//体重
      $user_info['blood_type']   =empty($res['blood_type'])?'':$res['blood_type'];//血型
      $user_info['feature_desc']=empty($res['feature_desc'])?'':$res['feature_desc'];//其他特征描述
      $user_info['adverse_effect']=empty($res['adverse_effect'])?'':$res['adverse_effect'];//药物过敏史  
    
  }
    return $user_info;
}

/**
 * 我的求助(发布历史列表)
 * @param int $user_id
 * @param int $page
 * @param int $page_size
 * @return array
 *
 * @author 
*/
function get_help($user_id,$page,$page_size)
{
	$user_sex = array('未知','男','女');
	$rescue_status = array('待救援','救援中','完成救援','已评价','取消');
	
	$sql = "select count(*) from ".$GLOBALS['db']->table('rescue_log')." where user_id='$user_id'";
	$count=$GLOBALS['db']->getOne($sql);
	$page_count = ($count > 0) ? intval(ceil($count / $page_size)) : 1;
  
	$info=array();
	$sql = "SELECT u.*,r.id,r.user_describe,r.status,r.incident_desc,r.rescue_type,r.add_time FROM " .
	$GLOBALS['db']->table('rescue_log'). " AS r LEFT JOIN ".$GLOBALS['db']->table('users').
	" AS u on r.user_id = u.user_id WHERE r.user_id='$user_id'";
	$res=$GLOBALS['db']->selectLimit($sql, $page_size, ($page-1) * $page_size);
  
	$list=array();
	while ($v=$GLOBALS['db']->fetchRow($res)) {
		$info['log_id'] = $v['id'];
		$info['status'] = $v['status'];
		$info['status_explain'] = $rescue_status[$v['status']];//救援状态
		$info['incident_desc'] = !empty($v['incident_desc'])?$v['incident_desc']:'';//现场描述
		$info['incident_type'] = get_rescue_typeName($v['rescue_type']);
		$info['add_time'] = date('Y-m-d H:i',$v['add_time']);
		$list[] = $info;
	}
	
   $pager = array(
              "total"  => $count,  
              "count"  => count($list),
              "more"   => $page < $page_count ? 1 : 0
			   );
			   
   $result = array('list' =>$list, 'pager' => $pager);
   return $result;
}
//我的救助
 function get_rescue($user_id,$page,$page_size){
    $user_sex = array('未知','男','女');
  $rescue_status = array('待救援','救援中','完成救援','已评价','取消');
  $count=$GLOBALS['db']->getOne("select count(*) from ".$GLOBALS['db']->table('rescue_log')." where rescue_id='$user_id'");
  $page_count = ($count > 0) ? intval(ceil($count / $page_size)) : 1;
  $info=array();
  $sql = "SELECT u.*,r.id,r.user_describe,r.status,r.incident_desc,r.rescue_id FROM " .$GLOBALS['db']->table('rescue_log').
  " AS r LEFT JOIN ".$GLOBALS['db']->table('users').
  " AS u on r.user_id = u.user_id WHERE r.rescue_id='$user_id'";
  $res=$GLOBALS['db']->selectLimit($sql, $page_size, ($page-1) * $page_size);
  $list=array();
  while ($v=$GLOBALS['db']->fetchRow($res)) {
    $info['log_id']=$v['id'];
    // $info['demander_id']=$v['user_id'];
    // $info['rescue_id']=$v['rescue_id'];
    $usericon = GZ_Api::get_url('/Mapi/public/images/usericon.png');
    $info['demander_headimg']=empty($v['headimg'])?$usericon:GZ_Api::get_url($v['headimg']);//头像
    $info['demander_name']=$v['user_name'];//姓名
    $info['demander_integral']=$v['user_integral'];
    $height=isset($v['height'])?$v['height']:'0.00';
    $weight=isset($v['weight'])?$v['weight']:'0.00';
    $info['demander_style']=$v['age'].'岁  '.$user_sex[$v['sex']].'  '.$height.'cm  '. $weight.'斤';
    $info['rescue_status']=$rescue_status[$v['status']];//救援状态
    $info['status']=$v['status'];
    $info['incident_desc']=isset($v['incident_desc'])?$v['incident_desc']:'';//现场描述
    $list[] = $info;
  }
   $pager = array(
              "total"  => $count,  
              "count"  => count($list),
              "more"   => $page < $page_count ? 1 : 0
          );
   $result = array('list' =>$list, 'pager' => $pager);
   return $result;
 
  
}

//钱包明细
 function wallet_account($uid){
  $data=array();
  $info=array();
  $user=$GLOBALS['db']->getRow("SELECT * FROM ".$GLOBALS['db']->table('users')." WHERE user_id='$uid' "); 
  $sql="SELECT * FROM ".$GLOBALS['db']->table('user_account')." WHERE user_id='$uid' and user_money!=0 order by log_id desc LIMIT 5";
  $res=$GLOBALS['db']->getAll($sql);
  foreach ($res as $k => $v) {
     $info[$k]['user_money']=$v['user_money'];
     $info[$k]['change_time']=$v['change_time'];
     $info[$k]['change_desc']=$v['change_desc'];
     $info[$k]['change_type']=$v['change_type'];
  }
  $data['user_money']=$user['user_money'];
  $data['account_info']=$info;
  return $data;
}

//收支明细
//type 1：金额 2：积分
 function get_account($uid,$type,$page, $page_size){
    $arr=array();
    if($type==1){
        $where="user_id='$uid' and user_money!=0";
    }else{
      $integral=$GLOBALS['db']->getOne("SELECT user_integral FROM ".$GLOBALS['db']->table('users')." WHERE user_id='$uid' "); 
      $where="user_id='$uid' and user_integral!=0";
    }
    $count=$GLOBALS['db']->getOne("SELECT count(*) FROM ".$GLOBALS['db']->table('user_account')." WHERE $where ");
    $page_count = ($count > 0) ? intval(ceil($count / $page_size)) : 1;
    $sql="SELECT * FROM ".$GLOBALS['db']->table('user_account')." WHERE $where order by log_id desc";
    $res=$GLOBALS['db']->selectLimit($sql, $page_size, ($page-1) * $page_size);
    $list=array();
    while ($row=$GLOBALS['db']->fetchRow($res)) {
        if($type==1){
          $arr['user_money']=$row['user_money'];
         }else{
          $arr['user_integral']=$row['user_integral'];
         } 
           $arr['change_time']=$row['change_time'];
           $arr['change_desc']=$row['change_desc'];
           $arr['change_type']=$row['change_type'];
           $list[] = $arr;
        }
     $pager = array(
              "total"  => $count,  
              "count"  => count($list),
              "more"   => $page < $page_count ? 1 : 0
          );
      if($type==1){
          $result = array('list' =>$list, 'pager' => $pager);
         }else{
          $result = array('integral'=>$integral,'list' =>$list, 'pager' => $pager);
      }
    
    return $result;
 }

 function get_mycard($uid){
    $info=array();
    $sql="SELECT * FROM ".$GLOBALS['db']->table('users').' where user_id='.$uid;
    $res=$GLOBALS['db']->getRow($sql);
    if($res){
        $info['headimg']=GZ_Api::get_url($res['headimg']);
        $info['user_name']=$res['user_name'];
        $now_time=time();
        if($res['card_type']==0||$res['card_over_time']<$now_time){
           $info['desc']='你还未订购套餐';
           $info['status']=0; 
        }else{
           $info['desc']=$res['card_over_time'];
           $info['status']=1;
        }
    }

   
   return $info;
 }

 function get_card(){
      $info=array();
      $sql='select * from '.$GLOBALS['db']->table('card').' where status=1';
      $res=$GLOBALS['db']->getAll($sql);
      foreach ($res as $k => $v) {
          $info[$k]['card_id']=$v['id'];
          $info[$k]['card_name']=$v['card_name'];
          $info[$k]['card_price']=$v['shop_price'];
      // $info[$k]['card_cover']=GZ_Api::get_url($v['card_cover']);
      }
      return $info;
 }

 function get_cardInfo($card_id){
     $sql='select card_name,title_name,shop_price,card_cover,content from '.$GLOBALS['db']->table('card').' where id='.$card_id;
     $res=$GLOBALS['db']->getRow($sql);
     $data=array();
     if($res){
		 $data['shop_price']= $res['shop_price'];
		 $data['title_name']= $res['title_name'];
		 $data['card_name']= $res['card_name'];
		 $data['content']= $res['content'];
        $data['card_cover']=GZ_Api::get_url($res['card_cover']);
     }
     
     return $data;
 }
 
 //救护类型名称
function get_rescue_typeName($id){
  $result=$GLOBALS['db']->getOne("select name from ".$GLOBALS['db']->table('rescue_type')." where id='$id'");
  return $result;
}
?>